package com.yswl.yeeduo.ui.tool;

import com.yswl.yeeduo.tool.KStringTool;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * 登录过后返回的参数 认证参数
 * 
 * @author Administrator
 * 
 */
public class ServiceAuthManager {

	/**
	 * 优先 app 是否第一次进入
	 */
	private static final String AUTH_STATE = "service_auth_state";
	private static final String TOKEN = "token";
	private static final String SECRET = "secret";
	private static final String USERID = "userId";

	/**
	 * 记录登录后服务端返回的认证参数 任意值
	 * 
	 * @param context
	 *            "token": "a34146a4bc7114336236f3b5c609eaefmJTVyMK6lL",
	 *            "secret": "9ec57b48fa457c990ea3ae48432bc2b0UGrrwseh1L",
	 *            "userId": "55fe9c9b57c63be3010041a7",
	 */
	public static void saveAuthParams(Context context, String token, String secret, String userId) {
		SharedPreferences sp = context.getSharedPreferences(AUTH_STATE, Activity.MODE_PRIVATE);
		Editor edit = sp.edit();
		edit.putString(TOKEN, token);//2ee936e5e127022d4b8d459f7314cc0bBeN61ckL59
		edit.putString(SECRET, secret);//756a29e9d07f59e9eb31c26b88c3382bPAOf23csA0
		edit.putString(USERID, userId);//56220601b9567db34c8b4965
		edit.commit();
	}
	
	
//	public static void saveAuthParamsUserId(Context context, String userId) {
//		SharedPreferences sp = context.getSharedPreferences(AUTH_STATE, Activity.MODE_PRIVATE);
//		Editor edit = sp.edit();
//		edit.putString(USERID, userId);
//		edit.commit();
//	}

	/**
	 * 获取token
	 * 
	 * @param context
	 * @return 9d935dc71d46e6f8d4f25ce527b2d9f3
	 */
	public static String getToken(Context context) {
		SharedPreferences sp = context.getSharedPreferences(AUTH_STATE, Activity.MODE_PRIVATE);
		String token = sp.getString(TOKEN, "9d935dc71d46e6f8d4f25ce527b2d9f3");
		token = KStringTool.isEmpty(token) ? "9d935dc71d46e6f8d4f25ce527b2d9f3" : token;
		return token;
	}

	/**
	 * 获取密钥 默认值和服务端统一
	 * @param context
	 * @return
	 */
	public static String getSecret(Context context) {
		SharedPreferences sp = context.getSharedPreferences(AUTH_STATE, Activity.MODE_PRIVATE);
		String secret = sp.getString(SECRET, "10aae2417bc74cfc4dd9b9b95e101e7b");
		secret = KStringTool.isEmpty(secret) ? "10aae2417bc74cfc4dd9b9b95e101e7b" : secret;
		return secret;
	}

	/**
	 * 获取 userId
	 * 
	 * @param context
	 * @return
	 */
	public static String getUserId(Context context) {
		SharedPreferences sp = context.getSharedPreferences(AUTH_STATE, Activity.MODE_PRIVATE);
		String userId = sp.getString(USERID, "");
		if (KStringTool.isEmpty(userId)) {
			return "";
		}
		return userId;
	}
}
